package com.wnxy.pawnbackgroundsystem.commons;


import lombok.extern.slf4j.Slf4j;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
@Slf4j
public class DateUtil {

    public static final String PATTEN = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTEN_NOT_TIME = "yyyy-MM-dd";

    //时间戳转化为Date
    public static Date getTimeStampToDate(Long timeStamp,String patten){
        SimpleDateFormat format = getSimpleDateFormat(patten);
        String time = format.format(timeStamp);
        ParsePosition pos = new ParsePosition(0);
        return format.parse(time,pos);
    }
    public static Date getTimeStampToDate(Long timeStamp) {
        return getTimeStampToDate(timeStamp,null);
    }
    //Sting时间格式转Date
    public static Date getStringTimeToDate(String timeFormat,String patten){
        SimpleDateFormat format = getSimpleDateFormat(patten);
        ParsePosition pos = new ParsePosition(0);
        return format.parse(timeFormat,pos);
    }
    public static Date getStringTimeToDate(String timeFormat){
        return getStringTimeToDate(timeFormat,null);
    }
    //String时间格式转时间戳
    public static Long getStringTimeToTimeStamp(String timeFormat,String patten){
        Date stringTimeToDate = getStringTimeToDate(timeFormat, patten);
        log.info("转换后的时间stringTimeToDate：{}",stringTimeToDate);
        return stringTimeToDate.getTime();
    }
    public static Long getStringTimeToTimeStamp(String timeFormat){
        return getStringTimeToTimeStamp(timeFormat, null);
    }
    //时间戳转化为StringTimeFormat
    public static String getTimeStampToString(Long timeStamp,String patten){
        SimpleDateFormat format = getSimpleDateFormat(patten);
        return format.format(timeStamp);
    }
    public static String getTimeStampToString(Long timeStamp) {
        return getTimeStampToString(timeStamp,null);
    }

    //时间戳转化为SimpleDateFormat
    public static SimpleDateFormat getSimpleDateFormat(String patten){
        String pat = getPatten(patten);
        return new SimpleDateFormat(pat);
    }

    //获取现在时间(String)
    public static String getStringDate(String patten) {
        String pat = getPatten(patten);
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(pat);
        return formatter.format(currentTime);
    }
    //获取现在时间（Date)
    public static Date getDate(){
        return new Date();
    }
    //获取Date转时间戳
    public static Long getSystemTimeStamp(){
        return getDate().getTime();
    }
    public static Long getDateToTimeStamp(Date date){
        return date.getTime();
    }
    private static String getPatten(String patten){
        String pat = PATTEN;
        if (patten != null) pat = patten;
        return pat;
    }
}
